﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;

namespace MyHealthOnline.Business
{
    public static class PatientBusiness
    {
        public static PatientCollection GetPatients()
        {
            PatientCollection patientColl = new PatientCollection();
            patientColl.GetMulti(null);
            return patientColl;
        }

        public static PatientEntity GetPatient(int patientId)
        {
            PredicateExpression filter = new PredicateExpression(PatientFields.Id == patientId);
            PatientCollection patientColl = new PatientCollection();
            patientColl.GetMulti(filter);

            if (patientColl.Count > 0)
            {
                return patientColl.ElementAt(0);
            }

            return null; 
        }

        public static PatientCollection GetPatients(string patientNo, string name, string birthDate, string gender)
        {
            
            PredicateExpression filter = new PredicateExpression();
            if(patientNo!="")
            {
                int no=Convert.ToInt32(patientNo);
                filter.Add(PatientFields.Id == no);
            }
            if(name!="")
                filter.Add(new FieldLikePredicate( PatientFields.Name ,"%"+name+"%"));

            if (birthDate != "")
                filter.Add(PatientFields.Birthday ==Convert.ToDateTime( birthDate));

            filter.Add(PatientFields.Sex == gender);
            
            PatientCollection patientColl = new PatientCollection();
            patientColl.GetMulti(filter);

            return patientColl;
        }

        public static PatientEntity FilterPatients(PatientCollection patientColl, int patientId)
        {
            PatientCollection newPatientColl = new PatientCollection();
            PredicateExpression filter = new PredicateExpression(PatientFields.Id == patientId);
            filter.Add(new FieldCompareRangePredicate(PatientFields.Id, patientColl.Items.Select(p => p.Id).ToArray()));
            newPatientColl.GetMulti(filter);

            if (newPatientColl.Count > 0)
                return newPatientColl.ElementAt(0);

            return null;
        }

        public static void CreatePatient(string name,string password, string number, string email, string address, string city,
                string country, string phone, string birthday, string gender)
        {
            DateTime dt;
            DateTime? ndt= null;
            if (DateTime.TryParse(birthday, out dt))
                ndt = dt;
            PatientEntity patient = new PatientEntity
            {
                Name=name,
                Number=Convert.ToInt32( number),
                Email=email,
                Address=address,
                City=city,
                Country=country,
                Phone=phone,
                Birthday= ndt,
                Sex=gender,
                Password=password
           //     Role="pacient"
            };
            patient.Save();
        }

        public static void DeletePatient(int patientId)
        {
            PredicateExpression filter = new PredicateExpression(PatientFields.Id == patientId);
            PatientCollection patientColl = new PatientCollection();
            patientColl.DeleteMulti(filter);
    
        }

        public static void UpdatePatient(int id, string name, string password, string number, string email, string address,
            string city, string country, string phone, string birthday, string sex)
        {
            DateTime dt;
            DateTime? ndt = null;
            if (DateTime.TryParse(birthday, out dt))
                ndt = dt;
            PatientEntity patient = GetPatient(id);
            if(patient==null)
                return;

            patient.Name = name;
            patient.Number = Convert.ToInt32(number);
            patient.Email = email;
            patient.Address = address;
            patient.City = city;
            patient.Country = country;
            patient.Phone = phone;
            patient.Birthday = ndt;
            patient.Sex = sex;
            patient.Password = password;
                //     Role="pacient"
            
            patient.Save();
        }
    }
}
